/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Admin_SelectItemsReport.java
 *
 * Created on Apr 7, 2011, 5:09:03 PM
 */
package admin_gui;

import admin.Controller.ItemReportController;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author edaype
 */
public class Admin_SelectItemsReport extends javax.swing.JPanel {

    private boolean[] itemItem;
    private String searchField;
    private Vector v_head;
    private JPanel mainPanel;
    private String UPC;
    private ItemReportController itemReportController;
    private DefaultListModel listModel;         //for the report generation
    private ArrayList<String> rlist;

    /** Creates new form Admin_SelectItemsReport
     * @param panel
     */
    public Admin_SelectItemsReport(JPanel panel) {
        initComponents();
        this.mainPanel = panel;
        v_head = new Vector();
        v_head.add("UPC");
        v_head.add("Name");
        v_head.add("Make");
        v_head.add("Model");
        searchField = null;
        itemReportController = new ItemReportController(mainPanel);
        itemItem = new boolean[5];
        itemItem[0] = true;
        for (int i = 1; i < 5; i++) {
            itemItem[i] = false;
        }
        listModel = new DefaultListModel();
        rlist = new ArrayList<String>();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TopPanel = new javax.swing.JPanel();
        SearchLabel = new javax.swing.JLabel();
        SearchField = new javax.swing.JTextField();
        OptionSearch = new javax.swing.JComboBox();
        GoButton = new javax.swing.JButton();
        ByLabel = new javax.swing.JLabel();
        ResultPane = new javax.swing.JScrollPane();
        ResultTable = new javax.swing.JTable();
        ButtonPanel = new javax.swing.JPanel();
        AddButton = new javax.swing.JButton();
        AddAllButton = new javax.swing.JButton();
        RemoveButton = new javax.swing.JButton();
        RemoveAllButton = new javax.swing.JButton();
        ReportPane = new javax.swing.JScrollPane();
        ReportList = new javax.swing.JList();
        TitlePanel = new javax.swing.JPanel();
        TitleLabel = new javax.swing.JLabel();
        ChartButton = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        TopPanel.setBackground(new java.awt.Color(255, 255, 255));
        TopPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        SearchLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        SearchLabel.setText("Search");

        SearchField.setFont(new java.awt.Font("Tahoma", 0, 24));

        OptionSearch.setFont(new java.awt.Font("Tahoma", 0, 24));
        OptionSearch.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Select--", "Name", "UPC", "Make", "Model" }));
        OptionSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OptionSearchActionPerformed(evt);
            }
        });

        GoButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        GoButton.setText("Go");
        GoButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GoButtonActionPerformed(evt);
            }
        });

        ByLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        ByLabel.setText(" By");

        javax.swing.GroupLayout TopPanelLayout = new javax.swing.GroupLayout(TopPanel);
        TopPanel.setLayout(TopPanelLayout);
        TopPanelLayout.setHorizontalGroup(
            TopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TopPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(SearchLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(SearchField, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(48, 48, 48)
                .addComponent(ByLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(OptionSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 114, Short.MAX_VALUE)
                .addComponent(GoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        TopPanelLayout.setVerticalGroup(
            TopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TopPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TopPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(SearchLabel)
                    .addComponent(SearchField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(GoButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ByLabel)
                    .addComponent(OptionSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        ResultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "UPC", "Name", "Model", "Make"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        ResultTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                ResultTableMousePressed(evt);
            }
        });
        ResultPane.setViewportView(ResultTable);
        ResultTable.getColumnModel().getColumn(1).setMinWidth(150);

        ButtonPanel.setBackground(new java.awt.Color(255, 255, 255));

        AddButton.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        AddButton.setText("Add");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        AddAllButton.setFont(new java.awt.Font("Tahoma", 0, 16));
        AddAllButton.setText("Add All");
        AddAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddAllButtonActionPerformed(evt);
            }
        });

        RemoveButton.setFont(new java.awt.Font("Tahoma", 0, 16));
        RemoveButton.setText("Remove");
        RemoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveButtonActionPerformed(evt);
            }
        });

        RemoveAllButton.setFont(new java.awt.Font("Tahoma", 0, 16));
        RemoveAllButton.setText("Remove All");
        RemoveAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveAllButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout ButtonPanelLayout = new javax.swing.GroupLayout(ButtonPanel);
        ButtonPanel.setLayout(ButtonPanelLayout);
        ButtonPanelLayout.setHorizontalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AddAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RemoveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RemoveAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(8, Short.MAX_VALUE))
        );
        ButtonPanelLayout.setVerticalGroup(
            ButtonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ButtonPanelLayout.createSequentialGroup()
                .addGap(81, 81, 81)
                .addComponent(AddButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(45, 45, 45)
                .addComponent(AddAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(47, 47, 47)
                .addComponent(RemoveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addComponent(RemoveAllButton, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(105, Short.MAX_VALUE))
        );

        ReportPane.setViewportView(ReportList);

        TitlePanel.setBackground(new java.awt.Color(255, 255, 255));

        TitleLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 43)); // NOI18N
        TitleLabel.setText("Sales Report (Item)");

        ChartButton.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        ChartButton.setText("Generate Chart");
        ChartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChartButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout TitlePanelLayout = new javax.swing.GroupLayout(TitlePanel);
        TitlePanel.setLayout(TitlePanelLayout);
        TitlePanelLayout.setHorizontalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 518, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 53, Short.MAX_VALUE)
                .addComponent(ChartButton)
                .addContainerGap())
        );
        TitlePanelLayout.setVerticalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(TitleLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ChartButton, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ResultPane, javax.swing.GroupLayout.PREFERRED_SIZE, 393, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ReportPane, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(TopPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(TitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TopPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ButtonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(ResultPane, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE)
                    .addComponent(ReportPane, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 478, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(TitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void OptionSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OptionSearchActionPerformed
        // TODO add your handling code here:
        JComboBox cb = (JComboBox) evt.getSource();
        String petName = (String) cb.getSelectedItem();
        //    System.out.println("Select Store "+petName);
        int index = cb.getSelectedIndex();
        itemItem[index] = true;
        for (int i = 0; i < 5; i++) {
            if (i != index) {
                itemItem[i] = false;
            }
        }
    }//GEN-LAST:event_OptionSearchActionPerformed

    private void GoButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GoButtonActionPerformed
        searchField = SearchField.getText();
        itemReportController.manageItem(ResultTable, v_head, searchField, itemItem);
    }//GEN-LAST:event_GoButtonActionPerformed

    private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
        // TODO add your handling code here:
        if (UPC == null) {
            System.out.println("UPC: " + UPC);
            JOptionPane.showMessageDialog(null, "Please select one record",
                    "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            int i = 0;
            for (i = 0; i < listModel.size(); i++) {
                if (listModel.getElementAt(i).toString().equals(UPC)) {
                    JOptionPane.showMessageDialog(null, "This record has been added to the list!",
                            "Error", JOptionPane.ERROR_MESSAGE);
                    break;
                }
            }
            if (i == listModel.size()) {
                listModel.addElement(UPC);
                ReportList.setModel(listModel);
                mainPanel.repaint();
            }
        }
    }//GEN-LAST:event_AddButtonActionPerformed

    private void ResultTableMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ResultTableMousePressed
        // TODO add your handling code here:
//        int column = ResultTable.getColumnModel().getColumnIndexAtX(evt.getX());
        int row = evt.getY() / ResultTable.getRowHeight();
        try {
            String result = (ResultTable.getValueAt(row, 0)).toString().trim();
            UPC = result;     //storeID here is in fact the store name
            //     System.out.println("storeName: "+result);
        } catch (NullPointerException e) {
        }
    }//GEN-LAST:event_ResultTableMousePressed

    private void AddAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddAllButtonActionPerformed
        // TODO add your handling code here:
        listModel.clear();
        int size = ResultTable.getRowCount();
        for (int i = 0; i < size; i++) {
            try {
                String storeN = (ResultTable.getValueAt(i, 0)).toString().trim();
                listModel.addElement(storeN);
            } catch (NullPointerException e) {
                JOptionPane.showMessageDialog(null, "None record!",
                        "Error", JOptionPane.ERROR_MESSAGE);
                break;
            }
        }
        ReportList.setModel(listModel);
        mainPanel.repaint();
    }//GEN-LAST:event_AddAllButtonActionPerformed

    private void RemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveButtonActionPerformed
        // TODO add your handling code here:
        boolean b = true;
        try {
            UPC = ReportList.getSelectedValue().toString();
        } catch (NullPointerException e) {
            b = false;
            //         System.out.println("UPC: "+UPC);
            JOptionPane.showMessageDialog(null, "Please select one record from the list",
                    "Error", JOptionPane.ERROR_MESSAGE);
        }
        if (b) {
            if (listModel.removeElement(UPC)) {
                ReportList.setModel(listModel);
                mainPanel.repaint();
            } else {
                JOptionPane.showMessageDialog(null, "No such record in the list!",
                        "Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_RemoveButtonActionPerformed

    private void RemoveAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveAllButtonActionPerformed
        // TODO add your handling code here:
        listModel.clear();
        ReportList.setModel(listModel);
        mainPanel.repaint();
    }//GEN-LAST:event_RemoveAllButtonActionPerformed

    private void ChartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChartButtonActionPerformed

        for (int i = 0; i < ReportList.getModel().getSize(); i++) {
            rlist.add(ReportList.getModel().getElementAt(i).toString());
        }
        itemReportController.showItemReport(rlist);
        //      System.out.println(rlist);
    }//GEN-LAST:event_ChartButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddAllButton;
    private javax.swing.JButton AddButton;
    private javax.swing.JPanel ButtonPanel;
    private javax.swing.JLabel ByLabel;
    private javax.swing.JButton ChartButton;
    private javax.swing.JButton GoButton;
    private javax.swing.JComboBox OptionSearch;
    private javax.swing.JButton RemoveAllButton;
    private javax.swing.JButton RemoveButton;
    private javax.swing.JList ReportList;
    private javax.swing.JScrollPane ReportPane;
    private javax.swing.JScrollPane ResultPane;
    private javax.swing.JTable ResultTable;
    private javax.swing.JTextField SearchField;
    private javax.swing.JLabel SearchLabel;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JPanel TitlePanel;
    private javax.swing.JPanel TopPanel;
    // End of variables declaration//GEN-END:variables
}
